function trimStr(str) { return str.replace(/(^\s*)|(\s*$)/g, ""); }
function addDate(dd, dadd) {
    var a = new Date(dd)
    a = a.valueOf()
    a = a + dadd * 24 * 60 * 60 * 1000
    a = new Date(a)
    return a;
}
//提示错误信息
function ShowError(ctrolID, message) {
    var ID = "Span" + ctrolID;
    if (message.length > 0) {
        if ($("#" + ID).css("display") == "none") {
            $("#" + ID).css("display", "inline");

        }
        $("#" + ID).css("color", "red");
        $("#" + ID).html(message);

    }
    else {
        if ($("#" + ID).css("display") == "inline") {
            $("#" + ID).css("display", "none");

        }
        $("#" + ID).html('');
    }

}
//获取url的参数 方法一
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return (r[2]); return null;

}
//返回日期加减
Date.prototype.DateAdd = function (strInterval, Number) {
    var dtTmp = this;
    switch (strInterval) {
        case 's': return new Date(Date.parse(dtTmp) + (1000 * Number));
        case 'n': return new Date(Date.parse(dtTmp) + (60000 * Number));
        case 'h': return new Date(Date.parse(dtTmp) + (3600000 * Number));
        case 'd': return new Date(Date.parse(dtTmp) + (86400000 * Number));
        case 'w': return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number));
        case 'q': return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number * 3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
        case 'm': return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
        case 'y': return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
    }
}
//日期转化成字符串
Date.prototype.toString = function (showWeek) {
    var myDate = this;
    var str = myDate.toLocaleDateString();
    if (showWeek) {
        var Week = ['日', '一', '二', '三', '四', '五', '六'];
        str += ' 星期' + Week[myDate.getDay()];
    }
    return str;
}
//用于在表中对时间进行格式化
function formatterdate(val, row) {

    if (val != null) {
        // var date = new Date(val);
        // var Str = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
        var Str = val.substring(0, 10);
        return Str;
    }
}

function formatterdatetime(val, length) {

    if (val != null) {
        // var date = new Date(val);
        // var Str = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
        var Str = val.substring(0, length);
        return Str;
    }
}
//function formatterstr(val) {//去掉中括号的方法
//    if (val != null) {
//        var strval = val.toString;
//        var length = strval.length;
//        var Str = strval.substring(1, length - 1);
//        return Str;
//    }
//}
function GetActionRights() {
    var Rights = "";
    $.ajax(
        {
            url: '../Ajax/SystemSettingHandler.ashx',
            type: 'post',
            data: { Flag: 'GetActionRights' },
            async: false,
            error: function () { $.messager.alert('出错啦！'); },
            success: function (data) {
                Rights = data;
            }
        }
    );

    return Rights;

}
//是否要移除某个页面某个元素（无权限就移除）
function IsRemoveElement(Code, Page) {
    var IsExist = IsInActionRights(Code, Page);
    if (IsExist == false) {
        $("#" + Code).remove();

    }

}

var ActionJson = "";
//检查某个页面某个元素是否有权限
function IsInActionRights(Code, Page) {

    if (ActionJson == "" || ActionJson == undefined) {
        ActionJson = GetActionRights();
    }

    if (ActionJson == "" || ActionJson == "[]") {
        ActionJson = "[]";
        return false;
    }
    if (ActionJson == "Success") {
        return true;
    }
    var ActionJson = JSON.parse(ActionJson);
    var Flag = false;
    $.each(ActionJson, function (n, value) {
            if (value.ActionCode == Code && value.URL == Page) {
                Flag = true;
                return false;

            }
        }
    );


    return Flag;
}

// 对Date的扩展，将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子：
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function (fmt) { //author: xxx
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}


//将easy UI 中日期控件转化成只能选择年和日期的控件，Id为页面上要转化的控件的Id
function SelectYearMonth(Id) {
    $('#' + Id).datebox({
        onShowPanel: function () {//显示日趋选择对象后再触发弹出月份层的事件，初始化时没有生成月份层
            span.trigger('click'); //触发click事件弹出月份层
            if (!tds) setTimeout(function () {
                //延时触发获取月份对象，因为上面的事件触发和对象生成有时间间隔
                tds = p.find('div.calendar-menu-month-inner td');
                tds.click(function (e) {
                    e.stopPropagation(); //禁止冒泡执行easyui给月份绑定的事件
                    var year = /\d{4}/.exec(span.html())[0]//得到年份
                        , month = parseInt($(this).attr('abbr'), 10); //月份
                    var Str = year + '-' + month;

                    $('#' + Id).datebox('hidePanel')//隐藏日期对象
                        .datebox('setValue', Str); //设置日期的值
                });
            }, 0)
        },
        parser: function (s) {
            if (!s) return new Date();
            var ss = (s.split('-'));
            var y = parseInt(ss[0], 10);
            var m = parseInt(ss[1], 10);
            var d = parseInt(ss[2], 10);
            if (!isNaN(y) && !isNaN(m)) {
                return new Date(y, m - 1, 1);
            } else {
                return new Date();
            }
        },
        formatter: function (date) {
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            var d = date.getDate();
            return y + '-' + (m < 10 ? ('0' + m) : m);

        }//配置formatter，只返回年月
    });
    var p = $('#' + Id).datebox('panel'), //日期选择对象
        tds = false, //日期选择对象中月份
        span = p.find('span.calendar-text'); //显示月份层的触发控件
}

//将数字四舍五入保留N位数字
var Digit = {};
Digit.round = function (digit, length) {
    length = length ? parseInt(length) : 0;
    if (length <= 0) return Math.round(digit);
    digit = Math.round(digit * Math.pow(10, length)) / Math.pow(10, length);
    return digit;
};

//使页面上所有的控件不能操作，大家可以继续扩展
function DisableControl() {
    $(":input").attr("disabled", true);
    $("a").attr("disabled", true);
    $(".easyui-combobox").combobox("disable");
    $(".easyui-datebox").combobox("disable");


}
function ReadonlyControl() {

    $(":input").attr("readonly", true);
    $("a").attr("readonly", true);
    $(".easyui-combobox").combobox("readonly");
    $(".easyui-datebox").combobox("readonly");

    $(".easyui-linkbutton").attr("disabled", true);
    $(".easyui-linkbutton1").attr("disabled", true);
    $(".easyui-linkbutton2").attr("disabled", true);
    $(".easyui-linkbutton3").attr("disabled", true);
    $(".easyui-combotree").combotree("readonly");

    $("input:radio").attr("disabled", true);
    $("input:checkbox").attr("disabled", true);
}

function FitDatagridHeight(tbGridID) {
    var total = document.documentElement.clientHeight;
    var RemoveHeight = 0;
    var Flag = getCookie("RomoveHeightFlag");
    if (Flag == "1") {
        RemoveHeight = 33;

    }
    else {
        RemoveHeight = 5;
    }
    var colHeight = total - RemoveHeight - document.getElementById(tbGridID).offsetTop + "px";
    $("#" + tbGridID).css('height', colHeight);
}

function FitDatagridHeight_3(tbGridID,td,n) {
    var total = document.documentElement.clientHeight;
    var RemoveHeight = 0;
    var Flag = getCookie("RomoveHeightFlag");
    if (Flag == "1") {
        RemoveHeight = 33;

    }
    else {
        RemoveHeight = 5;
    }
    var colHeight = total - RemoveHeight -30+ "px";
    $("#" + tbGridID).css('height', colHeight);
}

function FitDatagridHeight_2(tbGridID, h) {
    var total = document.documentElement.clientHeight;
    var RemoveHeight = 0;
    var Flag = getCookie("RomoveHeightFlag");
    if (Flag == "1") {
        RemoveHeight = 33;

    }
    else {
        RemoveHeight = 5;
    }
    var colHeight = total - RemoveHeight - h - document.getElementById(tbGridID).offsetTop + "px";
    $("#" + tbGridID).css('height', colHeight);
}

//按数字类型排序 自定义排序
function sort_int(a, b) {
    if (a.length > b.length) return 1;
    else if (a.length < b.length) return -1;
    else if (a > b) return 1;
    else return -1;
}
function getCookie(name) {
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (arr != null) return unescape(arr[2]); return null;
}
//只能选择当前时间之前year年内的日期
function LimitDate(DateBoxId, year, month, day) {
    $('#' + DateBoxId).datebox('calendar').calendar({

        validator: function (date) {
            var now = new Date();
            var d1 = new Date(now.getFullYear(), now.getMonth(), now.getDate());
            var d2 = new Date(now.getFullYear() - year, now.getMonth() - month, now.getDate() - day);
            return d1 >= date && date >= d2;

        }

    });

}

//只能选择当前日期之前的日期
function LimitCurrDate(DateBoxId) {
    $('#' + DateBoxId).datebox('calendar').calendar({

        validator: function (date) {
            var now = new Date();
            var d1 = new Date(now.getFullYear(), now.getMonth(), now.getDate());

            return d1 >= date;

        }

    });

}


function CloseParent() {
    var url = "./Index.aspx";
    var parentparentparent = window.parent.parent.parent;
    if (parentparentparent != null) {
        parentparentparent.location.replace(url);
    }
    else {
        var parentparent = window.parent.parent;
        if (parentparent != null) {
            parentparent.location.replace(url);
        }
        else {
            var parent = window.parent;
            if (parent != null) {
                parent.location.replace(url);
            }
            else {
                window.location.replace(url);
            }
        }
    }
}



/**
 * 功能 : //字符串 替换 占位符
 * 例子 :"我是{0},性别{1}".format("张三","男")
 * 结果 :"我是张三,性别男"
 * */
String.prototype.format = function () {
    var args = arguments;
    return this.replace(/\{(\d+)\}/g, function (s, i) {
        return args[i];
    });
}





//比较两个时间的大小关系
function W_CompareTwoDates(Startdate, Enddate) {
    var d1 = new Date(Startdate.replace(/\-/g, "\/"));
    var d2 = new Date(Enddate.replace(/\-/g, "\/"));
    if (d1 > d2) {
        return false;
    } else {
        return true;
    }
}

//获取系统当前时间到精确到日期
function W_getNowDate_Day() {
    var nowDate = new Date();
    var year = nowDate.getFullYear();
    var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1)
        : nowDate.getMonth() + 1;
    var day = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate
        .getDate();
    var dateStr = year + "-" + month + "-" + day;
    return dateStr;
}